﻿namespace WinForms
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormHelper
    {
        public void AddRow(DataGridView dg, int HjLieColNum)
        {
            if (dg.Rows.Count > 1)
            {
                this.DelRow(dg, dg.Rows.Count - 1);
            }
            dg.Rows.Add();
            dg.CurrentCell = dg.Rows[dg.Rows.Count - 1].Cells[0];
        }

        public void AddRowNoTotal(DataGridView dg)
        {
            dg.Rows.Add();
            dg.CurrentCell = dg.Rows[dg.Rows.Count - 1].Cells[0];
        }

        public void DelRow(DataGridView dg, int index)
        {
            dg.Rows.Remove(dg.Rows[index]);
        }

        public void EditRow(DataGridView dg, int HjLieColNum)
        {
            this.DelRow(dg, dg.Rows.Count - 1);
            this.TotalRow(dg, HjLieColNum);
        }

        public void EditRow(DataGridView dg, int HjLieColNum, string zsColName)
        {
            this.DelRow(dg, dg.Rows.Count - 1);
            this.TotalRow(dg, HjLieColNum, zsColName);
        }

        public decimal TotalRow(DataGridView dg, int HjLieColNum)
        {
            decimal num = 0M;
            dg.Rows.Add();
            DataGridViewRow row = dg.Rows[dg.Rows.Count - 1];
            row.ReadOnly = true;
            row.DefaultCellStyle.BackColor = Color.Khaki;
            row.Cells[HjLieColNum - 1].Value = "合计";
            try
            {
                for (int i = 0; i < (dg.Rows.Count - 1); i++)
                {
                    row.Cells[HjLieColNum].Value = Convert.ToSingle(row.Cells[HjLieColNum].Value) + Convert.ToSingle(dg.Rows[i].Cells[HjLieColNum].Value);
                }
                num = decimal.Parse(row.Cells[HjLieColNum].Value.ToString());
            }
            catch (Exception)
            {
                row.Cells[HjLieColNum].Value = "0.00";
            }
            return num;
        }

        public decimal TotalRow(DataGridView dg, int HjLieColNum, string zsColName)
        {
            decimal num = 0M;
            dg.Rows.Add();
            DataGridViewRow row = dg.Rows[dg.Rows.Count - 1];
            row.ReadOnly = true;
            row.DefaultCellStyle.BackColor = Color.Khaki;
            row.Cells[HjLieColNum - 1].Value = "合计";
            try
            {
                for (int i = 0; i < (dg.Rows.Count - 1); i++)
                {
                    if ((dg.Rows[i].Cells[zsColName].EditedFormattedValue.ToString() != "True") && (dg.Rows[i].Cells[zsColName].EditedFormattedValue.ToString() != "√"))
                    {
                        row.Cells[HjLieColNum].Value = Convert.ToSingle(row.Cells[HjLieColNum].Value) + Convert.ToSingle(dg.Rows[i].Cells[HjLieColNum].Value);
                    }
                }
                num = decimal.Parse(row.Cells[HjLieColNum].Value.ToString());
            }
            catch (Exception)
            {
                row.Cells[HjLieColNum].Value = "0.00";
            }
            return num;
        }
    }
}

